<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Popup window to enter external email address to forward selected
 * emails to. see: SBCCMCP28
 * http://tracker.remote-learner.net/browse/SBCCMCP-28
 *
 * @package   block_email_list
 * @author    Brent Boghosian <brent.boghosian@remote-learner.net>
 * @copyright 2011 Remote Learner - www.remote-learner.net
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once('../../../config.php');
require_once($CFG->dirroot.'/blocks/email_list/email/lib.php');
require_once($CFG->dirroot.'/blocks/email_list/email/email.class.php');

// AJAX functions
require_once($CFG->libdir. '/ajax/ajaxlib.php');

$courseid = required_param('courseid', PARAM_INT);
$mailidlist  = required_param('mailids', PARAM_SEQUENCE);

if (!$course = $DB->get_record('course', array('id', $courseid))) {
    print_error('invalidcourseid', 'block_email_list');
}

require_login($course->id, false); // No autologin guest

$PAGE->set_url('blocks/email_list/email/email2ext.php', array('courseid' => $courseid, 'mailids' => $mailidlist));
$PAGE->set_pagelayout('none');
$PAGE->set_title(get_string('forwardtoexternal', 'block_email_list'));
$PAGE->requires->css('blocks/email_list/email/email.css');

if (! email_is_enabled_email_list($course->id)) {
    print_error('blocknotavailable', 'block_email_list');
}

$context = context_course::instance($course->id);

//if (!$course->visible and has_capability('moodle/legacy:student', $context, $USER->id, false) ) {
if ( !$course->visible && !has_capability('moodle/course:viewhiddencourses', $context, $USER->id, false) ) {
    print_error('courseavailablenot', 'moodle');
}

echo $OUTPUT->header();
echo "<div id=\"page\" style=\"text-align: center\" >\n";
echo "<br />\n";

// create text input area for external email address and send/submit button
echo '<form method="post" name="fwdtoextemail" action="'.$CFG->wwwroot
     ."/blocks/email_list/email/sendmailext.php\" >\n";
echo "<input type=\"hidden\" name=\"courseid\" value=\"{$courseid}\" />\n";
echo "<input type=\"hidden\" name=\"mailids\" value=\"{$mailidlist}\" />\n";

echo "<table width=\"100%\" border=\"0\"><tr><td align=\"right\"><div class=\"header\"><label for=\"emailto\">".get_string('to','block_email_list').":&nbsp;</label></div></td><td align=\"left\"><input type=\"text\" name=\"emailto\" value=\"\" size=\"50\" maxlength=\"255\" />\n"; 
echo helpbutton('emailexternal', get_string('emailexternal', 'block_email_list'), 'block_email_list') . "</td></tr></table>\n";

echo '<br /><input type="submit" name="send" value="'.get_string('send', 'block_email_list');
echo '" onClick=\"return confirm(\''.get_string('suretosendext', 'block_email_list')
     ."') ? true: (window.close(),false);\" />\n";
echo ' <input type="button" name="cancel" value="'.get_string('cancel')."\" onClick=\"window.close();\" />\n";

echo "</form>\n";
echo "</div>\n";

echo $OUTPUT->footer();
